package com.learning.jquizenglish.common.fragment;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import android.util.Log;

import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.network.JQuizEnglishTask;
import com.learning.jquizenglish.network.connection.HttpUtil;
import com.learning.jquizenglish.network.connection.JsonUtil;


/**
 * The Class JQuizTaskFragment.
 */
public abstract class JQuizTaskFragment extends BaseFragment{

	/**
	 * Execute post task.
	 *
	 * @param <T> the generic type
	 * @param requestModel the request model
	 * @param isShowProgress the is show progress
	 * @param returnType the return type
	 * @param listener the listener
	 */
	public <T> void executePostTask(final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, T> jquizTask = new JQuizEnglishTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				T result = null;
				try {
					String json = "";
//					final String json = HttpUtil.getStringResponeByPostMethod(
//							(String) pParams[0], requestModel.listNameValuePairs);
					if (requestModel.multipartEntity!=null) {
						json = HttpUtil.getStringResponeByPostMultipart((String)pParams[0], requestModel.multipartEntity);
					}else{
						json = HttpUtil.getStringResponeByPostMethod(
								(String) pParams[0], requestModel.listNameValuePairs);
					}
					result = JsonUtil.convertObjectFromJsonString(json, returnType);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(T result) {
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(getActivity(), getContentView(), isShowProgress, requestModel.url);
	}
	
	public <T> void executeGetTask(final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, T> jquizTask = new JQuizEnglishTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				T result = null;
				try {
					final String json = HttpUtil.getStringResponseByGetMethod(pParams[0]);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
					result = JsonUtil.convertObjectFromJsonString(json, returnType);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(T result) {
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(getActivity(), getContentView(), isShowProgress, requestModel.url);
	}
	
	/**
	 * Execute post task return list.
	 *
	 * @param <T> the generic type
	 * @param requestModel the request model
	 * @param isShowProgress the is show progress
	 * @param returnType the return type
	 * @param listener the listener
	 */
	public <T> void executePostTaskReturnList(final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, ArrayList<T>> jquizTask = new JQuizEnglishTask<String, ArrayList<T>>() {

			@Override
			public ArrayList<T> processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				ArrayList<T> result = null;
				try {
					String json = "";
//					final String json = HttpUtil.getStringResponeByPostMethod(
//							(String) pParams[0], requestModel.listNameValuePairs);
					if (requestModel.multipartEntity!=null) {
						json = HttpUtil.getStringResponeByPostMultipart((String)pParams[0], requestModel.multipartEntity);
					}else{
						json = HttpUtil.getStringResponeByPostMethod(
								(String) pParams[0], requestModel.listNameValuePairs);
					}
					result = JsonUtil.convertArrayListFromJsonString(json, returnType);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(ArrayList<T> result) {
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(getActivity(), getContentView(), isShowProgress, requestModel.url);
	}
	
	public <T> void executeGetTaskReturnList(final JQuizRequestModel requestModel,boolean isShowProgress,final Class<T> returnType,final JQuizTaskListener listener){
		JQuizEnglishTask<String, ArrayList<T>> jquizTask = new JQuizEnglishTask<String, ArrayList<T>>() {

			@Override
			public ArrayList<T> processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				ArrayList<T> result = null;
				try {
					final String json = HttpUtil.getStringResponseByGetMethod(pParams[0]);
					Log.i("MobiNetworkJsonTask", " Json :" + json);
					result = JsonUtil.convertArrayListFromJsonString(json, returnType);
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
				return result;
			}

			@Override
			public void onUIUpDate(ArrayList<T> result) {
				if (getActivity() !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
		};
		jquizTask.run(getActivity(), getContentView(), isShowProgress, requestModel.url);
	}
	
	/**
	 * The listener interface for receiving JQuizTask events.
	 * The class that is interested in processing a JQuizTask
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addJQuizTaskListener<code> method. When
	 * the JQuizTask event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see JQuizTaskEvent
	 */
	public interface JQuizTaskListener{
		
		/**
		 * On task complete.
		 *
		 * @param object the object
		 */
		public void onTaskComplete(Object object);
	}
}
